##################################################
## Replication materials for 
## "Competent legislators or mere pawns? 
## Experimental evidence of attitudes toward gender quota politicians"
## by Carolyn Barnett, Alexandra Blackman, and Marwa Shalaby

## 5. Appendix: Additional Analyses
## This file creates Appendix Tables A.3 to A.4 and Appendix Figures A.3 to A.6

## NOTES: 
## - See file "01_setup.R" first to load necessary packages and data

# TABLE A.3: Effect of cross-party collaboration by politician gender and election type: Competence outcome -----------

# Effect of type of collaboration on competence
vign_h2_comp1 <- lm(vignette_competence ~ vignette_work, 
                    data = morvign[morvign$vignette_election=="Man/general",])
vign_h2_comp2 <- lm(vignette_competence ~ vignette_work, 
                    data = morvign[morvign$vignette_election=="Woman/general",])
vign_h2_comp3 <- lm(vignette_competence ~ vignette_work, 
                    data = morvign[morvign$vignette_election=="Woman/quota",])

# Output table format
stargazer(vign_h2_comp1,vign_h2_comp2,vign_h2_comp3,
          title = "Effect of cross-party collaboration by politician gender and election type: \\ Competence outcome",
          font.size = "scriptsize",
          keep.stat = c("n","adj.rsq"),
          model.names = F,
          multicolumn = TRUE)

# TABLE A.4: Effect of cross-party collaboration by politician gender and election type: Pawn-like outcome ------------

# Effect of type of collaboration on pawnlike
vign_h2_pawn1 <- lm(vignette_pawnlike ~ vignette_work, 
                    data = morvign[morvign$vignette_election=="Man/general",])
vign_h2_pawn2 <- lm(vignette_pawnlike ~ vignette_work, 
                    data = morvign[morvign$vignette_election=="Woman/general",])
vign_h2_pawn3 <- lm(vignette_pawnlike ~ vignette_work, 
                    data = morvign[morvign$vignette_election=="Woman/quota",])

# Output table format
stargazer(vign_h2_pawn1,vign_h2_pawn2,vign_h2_pawn3,
          title = "Effect of cross-party collaboration by politician gender and election type: \\ Pawn-like outcome",
          font.size = "scriptsize",
          keep.stat = c("n","adj.rsq"),
          model.names = F,
          multicolumn = TRUE)

# FIGURE A.3: Cooperativeness Outcome ----------------

# Transform dataset to long format 
morvign_long <- gather(morvign, measure, value, 
                       c(vignette_cooperation), 
                       factor_key=TRUE)

# Recode variables
morvign_long$vignette_election <- factor(morvign_long$vignette_election)
morvign_long$vignette_mode <- factor(morvign_long$vignette_mode)
morvign_long$vignette_work <- factor(morvign_long$vignette_work)
morvign_long$measure <- factor(morvign_long$measure, levels= c("vignette_competence", 
                                                               "vignette_cooperation", 
                                                               "vignette_pawnlike"))
# calculate differences in means
vign_election_coop <- summarySE(morvign_long, measurevar="value", 
                                 groupvars=c("measure","vignette_election"), na.rm= T)
vign_election_coop

# plot
ggplot(vign_election_coop, aes(x=measure, y=value, fill=vignette_election)) + 
  geom_bar(position=position_dodge(), stat="identity") +
  geom_errorbar(aes(ymin=value-ci, ymax=value+ci),
                width=.2,                    # Width of the error bars
                position=position_dodge(.9),  col = "black")+
  ylab("Outcome mean") + 
  scale_fill_manual(values = c("#666666", "#999999", "#CCCCCC")) + 
  coord_cartesian(ylim = c(4.1, 6.1)) +
  scale_x_discrete(labels=c("Cooperative")) +
  labs(x = "Outcome measures", 
       fill = "Politician Gender &\nMode of Election", 
       caption = "")

ggsave("vign_election_coop.png", plot = last_plot(),
       path = "Figures/", limitsize = F,
       width = 6, height = 6, units = "in", dpi =300)


# FIGURE A.4: Correlation of Competence and Cooperativeness Outcome Measures ---------

# set seed for reproducibility of jittered plots
set.seed(20482)

cor_comp_coop <- ggplot(morvign, aes(x = vignette_competence, 
                                     y = vignette_cooperation)) +
  geom_jitter() +
  facet_wrap(~vignette_election) +
  geom_smooth(method = "lm") +
  theme_bw() +
  labs(x = "Competence",
       y = "Cooperative")

cor_comp_coop

ggsave("vign_corr_comp_coop.png", plot = last_plot(),
       path = "Figures/", limitsize = F,
       width = 9, height = 4.7, units = "in", dpi = 300)

# FIGURE A.5: Correlation of Competence and Pawn-Like Outcome Measures -------------

# set seed for reproducibility of jittered plots
set.seed(20482)

cor_comp_pawn <- ggplot(morvign, aes(x = vignette_competence, 
                                     y = vignette_pawnlike)) +
  geom_jitter() +
  facet_wrap(~vignette_election) +
  geom_smooth(method = "lm") +
  theme_bw() +
  labs(x = "Competence",
       y = "Pawn-Like")

cor_comp_pawn

ggsave("vign_corr_comp_pawn.png", plot = last_plot(),
       path = "Figures/", limitsize = F,
       width = 9, height = 4.7, units = "in", dpi = 300)

# FIGURE A.6: Correlation of Cooperative and Pawn-Like Outcome Measures ---------------

# set seed for reproducibility of jittered plots
set.seed(20482)

cor_pawn_coop <- ggplot(morvign, aes(x = vignette_cooperation, 
                                     y = vignette_pawnlike)) +
  geom_jitter() +
  facet_wrap(~vignette_election) +
  geom_smooth(method = "lm") +
  theme_bw() +
  labs(x = "Cooperative",
       y = "Pawn-Like")

cor_pawn_coop

ggsave("vign_corr_pawn_coop.png", plot = last_plot(),
       path = "Figures/", limitsize = F,
       width = 9, height = 4.7, units = "in", dpi = 300)
